/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.JDOMException;

/**
 *
 * @author Ivan
 */
public class VirtualDirectory {

    String m_sName;// Virtual directory name
    String m_sPatch;// Physical path
    boolean m_bExist;// Physical path is existed

    public VirtualDirectory() {
        m_sName = null;
        m_sPatch = null;
        m_bExist = false;
    }

    public VirtualDirectory(String sName, String sPatch) {
        m_sName = sName;
        m_sPatch = sPatch;
        if(CheckDirectoryOrFile(m_sPatch, false) > 0)
            m_bExist = true;
        else
            m_bExist = false;
        
        String sTest= "test.aaa..bbb..txt";
        String test2 = sTest.substring(0, sTest.lastIndexOf("."));
        int i=0;
    }

    public String getName() {
        return m_sName;
    }

    public void setName(String sName) {
        m_sName = sName;
    }

    public String getPatch() {
        return m_sPatch;
    }

    public void setPatch(String sPatch) {
        m_sPatch = sPatch;
    }

     public boolean getExist() {
        return m_bExist;
    }
     
    public boolean getExistFileOrDirectory() {
        if(CheckDirectoryOrFile(m_sPatch, true) > 0)
            m_bExist = true;
        else
            m_bExist = false;
        return m_bExist;
    }

    /** 
     * If user request a virtual directory, return full physical path of the index of the directory.
     * If user request a file in virtual directory, return full physical path of the file.
     * If virtual directory is existed but physical path is not existed return virtual directory with existed = false
     * If virtual directory is not existed, return null.
     */
    public static VirtualDirectory getRealPath(String uri) throws IOException, JDOMException {
        try
        {
            //org.apache.commons.io
            //org.apache.commons.io.FileUtils
            // xử lý ...
            // có được realPath, name, isExisted
            
            
            String sRresult = "";
            String[] sSplitName = uri.split("/");
            System.out.println(sSplitName);
            int iSize = sSplitName.length;
            VirtualDirectory virFolder = new VirtualDirectory();
            List<VirtualDirectory> listVirFolder = XMLVirtualDirectory.GetALL();
            while(iSize>0)
            {
                sRresult=sSplitName[0];
                for(int i=1; i<iSize; i++)
                {
                    if(!sSplitName[i].equals(""))
                        sRresult = sRresult + "/" + sSplitName[i];
                }
                if(sRresult.startsWith("/") && sRresult.length() > 1)
                    sRresult = sRresult.substring(1, sRresult.length());

                for(int j=0; j<listVirFolder.size(); j++)
                {
                    if(sRresult.equals(listVirFolder.get(j).getName()))
                    {
                        virFolder = listVirFolder.get(j);
                        for(int h=iSize; h<sSplitName.length; h++)
                        {
                            virFolder.setPatch(virFolder.getPatch() + "\\" + sSplitName[h]);
                        }
                        int iType = virFolder.CheckDirectoryOrFile(virFolder.getPatch(), true);
                        if(iType == 1)
                        {
                            virFolder.setPatch(virFolder.getPatch() + "\\" + "index.html");
                            return virFolder;
                        }
                        else if(iType == 2)
                        {
                            return virFolder;
                        }
                        /*else//tìm file khi không gõ định dạng
                        {                
                            String sPatchFile = virFolder.getPatch().substring(0, virFolder.getPatch().lastIndexOf("\\") + 1);
                            String sNameFile =  virFolder.getPatch().substring(virFolder.getPatch().lastIndexOf("\\"), virFolder.getPatch().length());
                            File folder = new File(sPatchFile);
                            File[] listOfFiles = folder.listFiles();
                            for (int i = 0; i < listOfFiles.length; i++) {
                                if (listOfFiles[i].isFile() && listOfFiles[i].getName().substring(0, listOfFiles[i].getName().lastIndexOf(".")).equals(sNameFile)) {
                                    virFolder.setPatch(sPatchFile + listOfFiles[i].getName());
                                    return virFolder;
                                }
                            }
                        }*/
                        return new VirtualDirectory();
                    }
                }
                iSize = iSize -1;
            }
            return virFolder;
        }
        catch(Exception ex)
        {
            return new VirtualDirectory();
        }
    }

    public int CheckDirectoryOrFile(String sPatchFolder, boolean bAndFile) {
        try {
            File fFolder = new File(sPatchFolder);
            if (fFolder.exists())
            {
                if(fFolder.isDirectory())
                {
                    fFolder.setReadOnly();
                    return 1;
                }else if(bAndFile == true && fFolder.isFile())
                {
                    return 2;
                }
            }
            return 0;
        } catch (Exception ex) {
            //System.out.println("-ERR CheckDirectory");
            return 0;
        }
    }
}
